package belf.migrate.engine.job.structure;

import belf.migrate.api.taskconf.JobContext;
import belf.migrate.api.taskconf.TaskConf;
import belf.migrate.api.type.LogLevelType;
import belf.migrate.api.type.MigrationObjectType;
import belf.migrate.api.util.PostLog;
import belf.migrate.api.job.Job;
import belf.migrate.engine.job.JobResult;
import belf.migrate.engine.job.structure.helper.*;
import belf.migrate.api.taskconf.Task;
import com.google.gson.JsonArray;

import static belf.migrate.api.job.JobCode.EXECUTING;
import static belf.migrate.api.job.JobCode.SUCCESS;

public class DropAllStucture {
    private Job job;
    private Task task;
    private TaskConf taskConf;
    private JobContext jobContext;

    public DropAllStucture(Job job) {
        this.job = job;
        this.task = job.getTask();
        this.taskConf = task.getTaskConf();
        this.jobContext = new JobContext(job.getJobId().toString(), taskConf);
    }

    public JobResult execute() {
        PostLog.sendLogModel(
                job.getJobId(),
                job.getTask().getTaskType().getName(),
                "删除所有结构开始！",
                LogLevelType.INFO.getType(),
                EXECUTING,
                jobContext.getProgress());

        JobResult executedResult = new JobResult(SUCCESS, "删除所有结构开始Job提交成功。");
        JsonArray items = new JsonArray();
        StructureJobRunner structureJobRunner = new StructureJobRunner(job, false, false, true);
        //初始化JobContext
        structureJobRunner.check();
        this.jobContext = structureJobRunner.getJobContext(); //所有子任务复用一个jobContext

        JobResult procedure = new DropProcedureTool(job, jobContext).run();
        procedure.setTypeName(MigrationObjectType.PROCEDURE.getName());
        items.add(procedure.toJson());

        JobResult function = new DropFunctionTool(job, jobContext).run();
        function.setTypeName(MigrationObjectType.FUNCTION.getName());
        items.add(function.toJson());

        JobResult trigger = new DropTriggerTool(job, jobContext).run();
        trigger.setTypeName(MigrationObjectType.TRIGGER.getName());
        items.add(trigger.toJson());

        JobResult view = new DropViewTool(job, jobContext).run();
        view.setTypeName(MigrationObjectType.VIEW.getName());
        items.add(view.toJson());

        JobResult table = new DropTableTool(jobContext, job).run();
        table.setTypeName(MigrationObjectType.TABLE_STRUCTURE.getName());
        items.add(table.toJson());

        JobResult type = new DropTypeTool(job, jobContext).run();
        type.setTypeName(MigrationObjectType.TYPE.getName());
        items.add(type.toJson());

        JobResult seq = new DropSeqTool(job, jobContext).run();
        seq.setTypeName(MigrationObjectType.SEQUENCE.getName());
        items.add(seq.toJson());

        JobResult synonym = new DropSynonymTool(job, jobContext).run();
        synonym.setTypeName(MigrationObjectType.SYNONYM.getName());
        items.add(synonym.toJson());

        PostLog.sendLogModel(
                job.getJobId(),
                job.getTask().getTaskType().getName(),
                "删除所有结构结束！",
                LogLevelType.INFO.getType(),
                EXECUTING,
                jobContext.getProgress());
        executedResult.setData(items);
        return executedResult;
    }
}
